//
//  device_retriever.cpp
//  testpcap
//
//  Created by bugforever on 1/10/15.
//  Copyright (c) 2015 bugforever. All rights reserved.
//
#include "device_retriever.h"
#include "string.h"

#ifdef _WIN32
#include "device_retriever_windows.h"
#else
#include "device_retriever_libpcap.h"
#endif


device_info::device_info()
: m_str_name(0)
, m_str_friendlyname(0)
, m_str_description(0)
{
    ;
}

device_info::~device_info()
{
    delete[]m_str_name;
    delete[]m_str_friendlyname;
}

void device_info::set_name(char * str_name)
{
    if( !str_name )
        return;
    
    cpy_string(&m_str_name, str_name);
}

void device_info::set_friendlyname(char * str_friendlyname)
{
    if( !str_friendlyname )
        return;
    
    cpy_string(&m_str_friendlyname, str_friendlyname);
}

void device_info::set_description(char * str_description)
{
    if( !str_description )
        return;
    
    cpy_string(&m_str_description, str_description);
}

char * device_info::get_name()
{
    return m_str_name;
}

char * device_info::get_friendlyname()
{
    return m_str_friendlyname;
}

char * device_info::get_description()
{
    return m_str_description;
}

device_info& device_info::operator=( device_info& info )
{
    if( this == &info )
        return *this;
    
    this->set_name( info.get_name() );
    this->set_friendlyname( info.get_friendlyname() );
    this->set_description( info.get_description() );
    
    return *this;
}

void device_info::cpy_string(char ** ptr_str_dest, char * str_src)
{
    delete[] * ptr_str_dest;
    
    int len = (int)strlen(str_src);
    
    *ptr_str_dest = new char[len + 1];
    memset(*ptr_str_dest, 0, len + 1);
    
#ifdef _WIN32
    strcpy_s(*ptr_str_dest, len + 1, str_src);
#else
    strcpy(*ptr_str_dest, str_src);
#endif
}

device_retriever::device_retriever()
:m_ptr_retriever(0)
{
#ifdef _WIN32
    m_ptr_retriever = new device_retriever_windows();
#else
    m_ptr_retriever = new device_retriever_libpcap();
#endif
}

device_retriever::~device_retriever()
{
    delete m_ptr_retriever;
}

bool device_retriever::get_device_list(std::vector< device_info * > ** pptr_list)
{
    return m_ptr_retriever->get_device_list( pptr_list );
}


